package mao.leetcode.others.leetcode199;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> list = new ArrayList<>();
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        node1.right = node2;
       node2.left = node3;
}

    public List<Integer> rightSideView(TreeNode root) {
        helper(root,0);
        return list;
    }
    public void helper(TreeNode node,int level){
        if (node == null ) return;
        if (list.size() == level) list.add(node.val);
        helper(node.right,level+1);
        helper(node.right,level+1);
    }
    public List<Integer> rightSideView_1(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        q.offer(root);
        TreeNode node = null;
        while ( !q.isEmpty()){
            int size = q.size();
            for (int i = 0; i < size; i++) {
                node = q.poll();
                if (node.left != null) q.offer(node.left);
                if (node.right != null) q.offer(node.right);
            }
            res.add(node.val);
        }
        return res;
     }

    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
}
